// packageH/sign_shopV2/index/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    esignData: {},
    showPersonBank: false,
    showCompany: false,
    showControl: false,//完善合同，填写控件信息弹窗
    changeCompany: false,
    is_new_add_com: false,//是否为新增企业

    controlInfo: {},
    hasCompany: false,//合同列表是否有企业需要认证
    cid: 0,
    scene: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.init();
    this.getData();
  },

  init(){
    this.setData({
      scene: this.options.scene,
      hasCompany: false,
      hasSigning: false,
      is_new_add_com: false,
      order_id: this.options.order_id
    })
  },
  getData(){
    let urlStr = app.getNetAddresss("plugin.shop-esign-v2.frontend.controllers.index.index");
    app._postNetWork({
      url: urlStr,
      data: {scene_type: this.options.scene ,cid: this.data.cid,order_id: this.options.order_id},
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            esignData: res.data,
            cid: res.data.cid||0
          })
          this.forSceneList(res.data.scene_list);
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },
  forSceneList(list){
    list.forEach(item => {
      if(item.sign_type == 1) {
        this.setData({
          hasCompany: true //有企业签署，需要显示企业认证
        })
      } 
      if(!item.signed){
        this.setData({
          hasSigning: true //有合同还没完成签署
        })
      }
    });
  },
  toDecertification(e){
    let {status,val} = e.currentTarget.dataset;
    console.log(e)
    if(val == 'showCompany' && this.data.esignData.person_status == 0) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请先完成个人认证',
        success (res) {
        }
      });
      return;
    }
    if(!Number(status)) {
      this.setData({
        [val]: true
      })
    };
  },
  changeCid(e){
    let data = e.detail;
    if(data.cid){
      this.setData({
        cid: data.cid,
        is_new_add_com: false
      })
    }
    this.getData();
  },
  addCompany(){
    this.setData({
      is_new_add_com: false,
      showCompany: true
    })
  },
  toSign(e) {
    let {item} = e.currentTarget.dataset;
    if(this.data.esignData.person_status == 0) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请先完成个人认证'
      });
      return;
    }
    if(this.data.hasCompany && !this.data.esignData.company_status){
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请先完成企业认证',
        success (res) {
        }
      });
      return;
    }
    
    this.setData({
      controlInfo: item,
      showControl: true
    })
  },
  confirmSign(){
    if(!this.data.esignData.person_status){
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请先完成个人认证'
      });
      return;
    }
    if(this.data.hasCompany && !this.data.esignData.company_status){
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请先完成企业认证'
      });
      return;
    }
    if(this.data.hasSigning) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请完成合同签署'
      });
      return;
    }
    if(this.options.scene == 3) {
      wx.redirectTo({
        url: '/packageB/member/love/love_transfer/love_transfer',
      });
      return;//爱心值转账
    }
    if(this.options.scene == 2) {
      wx.redirectTo({
        url: '/packageA/member/withdrawal/withdrawal',
      });
      return;//提现
    }
    if(this.options.scene == 1) {
      wx.redirectTo({
        url: `/packageD/buy/orderPay/orderPay?order_id=${this.options.order_id}&status=2`,
      });
      return;//支付订单
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})